<?php
namespace app\admin\controller;

use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

class Index extends Base
{
    public function index(){
        $this->set_tem_val();
        $list = db('company')->where(['is_del'=>0])->order('id desc')->paginate('',false,page_param());
        $arr_list = $list->items();
        $addr_ids = array_unique(array_merge(array_column($arr_list, 'province_id'), array_column($arr_list, 'city_id'), array_column($arr_list, 'area_id')));
        $region_list = db('region')->where('id', 'in', $addr_ids)->column('id, name');
        $this->assign('list', $list);
        $this->assign('region_list', $region_list);

        //服务器信息配置
        $systemConfig = $this->systemConfig();
        $this->assign('systemConfig', $systemConfig);
        return $this->fetch();
    }

    //获取服务器配置信息
    private function systemConfig(){
        return $config = [
            '操作系统' => PHP_OS,
            '企业数量'=>db('company')->where('is_del',0)->count(),
            '用户数量'=>db('users')->count('id'),
        ];
    }

    /**
     * 发送通知
     */
    public function send_msg(){
        $param = input();
        $list = db('company')->where('id', 'in', $param['id'])->field('id, start_time, use_time')->select();
        $now_time = time();
        foreach($list as $k=>$v){
            $expire_time = date('Y-m-d',strtotime("+".$v['use_time']."year",$v['start_time']));
            $r_time = time_to_str(strtotime($expire_time) - $v['start_time']);
            $insert_data = [
                'com_id'=>$v['id'],
                'title'=>'剩余时间提醒',
                'content'=>"企业剩余时间 {$r_time}",
                'create_time'=>$now_time
            ];
            db('com_message')->insert($insert_data);
        }
        return $this->ajaxReturn(0,"成功", url(CONTROLLER_NAME.'/index'));
    }

    public function test(){
        $this->exportExcel();
        echo "1233";exit;
        //生成excel
    }

    //导出为excel
    //https://www.helloweba.net/php/563.html
    public function exportExcel($list=""){
        $list = [
            [
                "name"  =>  "张三",
                "yuwen" =>  "12345678901234567890",
                "shuxue"=>  "12345678901234567890",
            ],
            [
                "name"  =>  "李四",
                "yuwen" =>  "98765432109876543210",
                "shuxue"=>  "98765432109876543210",
            ],
        ];
        $spreadsheet=   new Spreadsheet();
        $worksheet  =   $spreadsheet->getActiveSheet();
        //设置工作表标题
        $worksheet->setTitle("测试生成excel");
        //设置单元格
        $worksheet->setCellValueByColumnAndRow(1, 1, '学生成绩表');
        $worksheet->setCellValueByColumnAndRow(1, 2, '姓名');
        $worksheet->setCellValueByColumnAndRow(2, 2, '语文');
        $worksheet->setCellValueByColumnAndRow(3, 2, '数学');

        //合并单元格
        $worksheet->mergeCells('A1:E1');
        $styleArray = [
            'font' => [
                'bold' => true
            ],
            'borders'   =>  [
                'allBorders'    =>  [
                    'borderStyle'   =>  Border::BORDER_THIN,
                    'color'         =>  ['argb' =>  '666666'],
                ],
            ],
            'alignment' => [
                'horizontal' => Alignment::HORIZONTAL_CENTER,//横向居中
            ],
        ];
        $worksheet->getStyle('A1')->applyFromArray($styleArray)->getFont()->setSize(28);
        $worksheet->getStyle('A2:E2')->applyFromArray($styleArray)->getFont()->setSize(14);
//        $writer= new Xlsx($spreadsheet);
        $writer=    IOFactory::createWriter($spreadsheet,"Xlsx");
        $writer->save("test.xlsx");
    }


    /**
     * 获取省市区
     */
    public function get_addr(){
        $pid = input("pid", 0); //上级id
        $def_id = input('def_id', 0); //默认选中id
        $is_html = input('is_html', 1); //1以html形式返回 0返回json
        $addr_list = db('region')->where('pid', $pid)->field('id, name')->select();
        if($is_html){
            $tem = '';
            foreach($addr_list as $k=>$v){
                if($v['id'] == $def_id){
                    $tem .= "<option value='{$v['id']}' selected>{$v['name']}</option>";
                }else{
                    $tem .= "<option value='{$v['id']}'>{$v['name']}</option>";
                }
            }
            $addr_list = $tem;
        }
        return json($addr_list);
    }


}
